<?php
	include 'open-connection.php';

	// Performing SQL query
	$query = 'SELECT consulta.*,
	patologia.idpatologia as idpatologia, patologia.nombre as patologia,
	medico.idmedico as idmedico, medico.nombres as medico,
	paciente.idpaciente as idpaciente, paciente.nombres as paciente
	FROM consulta
	INNER JOIN patologia ON consulta.idpatologia = patologia.idpatologia
	INNER JOIN medico ON consulta.idmedico = medico.idmedico
	INNER JOIN paciente ON consulta.idpaciente = paciente.idpaciente
	LIMIT '.$_POST['start'].', '.$_POST['limit'].'';
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$arr[] = $line;
	}
	$total = mysql_num_rows(mysql_query('SELECT * FROM consulta'));

	include 'close-connection.php';
	
	//$arr1 = array_chunk($arr, 25);
	echo '{total: '.$total.', results: '.json_encode($arr).'}';
?>